package com.zy.mc_module.utils;

import java.math.BigDecimal;

/**
 * author : dq
 * date   : 19/4/15
 * desc   : 金额工具类
 */
public class PriceUtils {

    public static double add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    /***
     * 去掉小数点后面多余的0
     */
    public static String removeTrim(Object object) {
        try {
            String str = object.toString();
            if (str.indexOf(".") > 0) {
                str = str.replaceAll("0+?$", "");//去掉多余的0
                str = str.replaceAll("[.]$", "");//如最后一位是.则去掉
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }

    /***
     * 小数点保留两位小数
     */
    public static String decimalsT(Object object) {
        try {
            String str = object.toString();
            BigDecimal bd = new BigDecimal(str);
            return bd.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "0.00";
        }
    }
}
